216D - Spider's Web - CodeForces Solution


binary search sortings two pointers *1700

Please click on ads to support us..

C++ Code:

#define IB ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#include <bits/stdc++.h>

#define ll long long
#define loop(i, n)   for(int i=0;i<n;i++)
#define loopi(i, j, n) for(int i=j;i<n;i++)
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
const int MAX = 1e9, MOD = 1e9 + 7, N = 1e5 + 5;
const double eps = 1e-10;
// L-R-U-D-DL-DR-UL-UR
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};
vector<vector<int>> cells;
int n;

int sol() {
    int unstable = 0;
    for (int i = 0; i < n; ++i) {
        int adj1 = (i + 1 == n ? 0 : i + 1);
        int adj2 = (i - 1 == -1 ? n - 1 : i - 1);
        for (int j = 1; j < cells[i].size(); ++j) {
            int e_adj1 = upper_bound(all(cells[adj1]), cells[i][j]) - cells[adj1].begin();
            int e_adj2 = upper_bound(all(cells[adj2]), cells[i][j]) - cells[adj2].begin();
            int s_adj1 = lower_bound(all(cells[adj1]), cells[i][j - 1]) - cells[adj1].begin();
            int s_adj2 = lower_bound(all(cells[adj2]), cells[i][j - 1]) - cells[adj2].begin();

            if (e_adj1 - s_adj1 != e_adj2 - s_adj2) unstable++;
        }
    }
    return unstable;
}

void solve() {
    int _ = 1;
    //cin >> _;
    while (_--) {
        int k;
        cin >> n;
        cells.resize(n);
        loop(i, n) {
            cin >> k;
            cells[i].resize(k);
            loop(j, k) cin>>cells[i][j];
            sort(all(cells[i]));
        }
        cout << sol() << endl;
    }
}

void Hema() {
    IB
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#endif
    solve();
}

int main() {
    Hema();
}

	 	 	 			     		   			 	  				


Comments

Submit
0 Comments
More Questions

Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal